package com.swinw.asumall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.swinw.asumall.product.dao.BrandDao;
import com.swinw.asumall.product.dao.CategoryDao;
import com.swinw.asumall.product.entity.AttrEntity;
import com.swinw.asumall.product.entity.BrandEntity;
import com.swinw.asumall.product.entity.CategoryEntity;
import com.swinw.asumall.product.service.BrandService;
import com.swinw.asumall.product.service.CategoryService;
import com.swinw.asumall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.Query;

import com.swinw.asumall.product.dao.CategoryBrandRelationDao;
import com.swinw.asumall.product.entity.CategoryBrandRelationEntity;
import com.swinw.asumall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryBrandRelationDao relationDao;

    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询品牌名和分类名
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);

    }

    @Transactional
    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandName(name);
        entity.setBrandId(brandId);
        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("brand_id",brandId);
        this.update(entity,updateWrapper);
    }

    @Transactional
    @Override
    public void updateCascad(Long catId, String name) {
        baseMapper.updateCascad(catId,name);
    }

    @Override
    public List<BrandVo> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        Map<Long,String> map = new HashMap<>();//利用集合对查询出来的数据进行去重
        catelogId.forEach((item)->{
            map.put(item.getBrandId(),item.getBrandName());
        });

        List<BrandVo> collect = new ArrayList<>();//将去重的数据封装为BrandVo
        map.forEach((key,name)->{
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(key);
            brandVo.setBrandName(name);
            collect.add(brandVo);
        });
        return collect;
    }

}